<html>
<head><meta charset="utf-8"><title>Why is Drop run at end of lexical scope? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html">Why is Drop run at end of lexical scope?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="246835016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246835016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246835016">(Jul 22 2021 at 11:35)</a>:</h4>
<p>This is a sort of general language question but I was wondering if anyone has a clear example / explanation of why Drop is done at the end of a lexcial scope rather than eagerly when a value stops being live? It seems to be related to ~unsafe~ code but I have a hard time understanding what code would be sound with drop at the end of a lexical scope and unsound otherwise..</p>



<a name="246836230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836230">(Jul 22 2021 at 11:51)</a>:</h4>
<p>A simple example would be drop guards.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">_guard</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mutex</span><span class="p">.</span><span class="n">lock</span><span class="p">();</span><span class="w"></span>
<span class="w">  </span><span class="c1">// critical section</span>
<span class="w">  </span><span class="c1">// ...</span>
<span class="w">  </span><span class="c1">// end, guard dropped here</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="246836485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836485">(Jul 22 2021 at 11:54)</a>:</h4>
<p>hmm ok, but that pattern seems to happen <em>because</em> drop runs at the end of a scope. if we had it run 'eagerly' then we could do things like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">mutex</span><span class="p">.</span><span class="n">lock</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>

<span class="p">});</span><span class="w"></span>
</code></pre></div>



<a name="246836619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836619">(Jul 22 2021 at 11:56)</a>:</h4>
<p>Alternatives exist, but existing code has to keep working.</p>



<a name="246836667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836667">(Jul 22 2021 at 11:56)</a>:</h4>
<ul>
<li>It is more predictable, so it avoids potential footguns. </li>
<li>Before NLLs this didn't make much sense (even borrows lasted until the end of the scope), now it is a breaking change (not that we really want that)</li>
</ul>



<a name="246836738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836738">(Jul 22 2021 at 11:57)</a>:</h4>
<p>I think I have seen some proposal allowing more eager drops in some circumstances. I think it was with an annotation or something? But I can't find it right now.</p>



<a name="246836750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836750">(Jul 22 2021 at 11:57)</a>:</h4>
<p>That gets annoying if you have multiple resources (two locks, two open files etc.) because you end up nesting closures.</p>



<a name="246836790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836790">(Jul 22 2021 at 11:57)</a>:</h4>
<p>tying functional behaviour to borrowck would also prevent improvements to borrowck in the first place.</p>



<a name="246836869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836869">(Jul 22 2021 at 11:58)</a>:</h4>
<p>Closure APIs are also annoying when you need to e.g. initialize a local variable</p>



<a name="246836888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836888">(Jul 22 2021 at 11:58)</a>:</h4>
<p><span class="user-mention silent" data-user-id="338379">Giacomo Stevanato</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246836869">said</a>:</p>
<blockquote>
<p>Closure APIs are also annoying when you need to e.g. initialize a local variable</p>
</blockquote>
<p>Or an early <code>return</code>.</p>



<a name="246836951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836951">(Jul 22 2021 at 11:59)</a>:</h4>
<p>I'm sure we'd come up with something like <code>with</code> statement in python if we needed a nice way to extend liveness of a resource.</p>



<a name="246836965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246836965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246836965">(Jul 22 2021 at 11:59)</a>:</h4>
<p>or perhaps just tell people to put <code>drop(x)</code> in the right places.</p>



<a name="246837068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837068">(Jul 22 2021 at 12:00)</a>:</h4>
<p>(my personal opinion is that you want <code>drop(...)</code> anyway where it matters, even if it doesn't make a functional difference _right now_)</p>



<a name="246837115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837115">(Jul 22 2021 at 12:01)</a>:</h4>
<p>these are all good and convincing arguments</p>



<a name="246837120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837120">(Jul 22 2021 at 12:01)</a>:</h4>
<p>Anyway an example of <code>unsafe</code> code that's currently sound but would be unsound if variables were eagerly dropped:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">];</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">v</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">();</span><span class="w"></span>
<span class="c1">// Now `v` is no longer used, can it be dropped?</span>
<span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// This would be unsound if `v` was already dropped</span>
</code></pre></div>



<a name="246837149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837149">(Jul 22 2021 at 12:01)</a>:</h4>
<p>I was curious because it seems like the consequence of the current drop approach is that actually implementing Drop means you are riding the knife's edge of soundness</p>



<a name="246837170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837170">(Jul 22 2021 at 12:01)</a>:</h4>
<p>since you may be interacting with data that has already been deallocated (ie <code>may_dangle</code>)</p>



<a name="246837241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837241">(Jul 22 2021 at 12:02)</a>:</h4>
<p><code>may_dangle</code> is a promise you're not interacting with that data</p>



<a name="246837268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837268">(Jul 22 2021 at 12:02)</a>:</h4>
<p>yea but its not enforced by the compiler, right?</p>



<a name="246837287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837287">(Jul 22 2021 at 12:02)</a>:</h4>
<p>Also, it's unstable and requires an <code>unsafe impl</code></p>



<a name="246837324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837324">(Jul 22 2021 at 12:03)</a>:</h4>
<p>No, but it's an <code>unsafe</code> contract,</p>



<a name="246837485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837485">(Jul 22 2021 at 12:05)</a>:</h4>
<p>yea, of course, but then someone like me has to go prove it sound <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="246837628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837628">(Jul 22 2021 at 12:06)</a>:</h4>
<p>If <code>drop</code> was called eagerly you would have to prove that a lot more code is sound though</p>



<a name="246837649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837649">(Jul 22 2021 at 12:06)</a>:</h4>
<p>6 of one, half a dozen of the other...</p>



<a name="246837653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837653">(Jul 22 2021 at 12:06)</a>:</h4>
<p>Well, you still have to prove it, but it would become more difficult and error prone</p>



<a name="246837670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837670">(Jul 22 2021 at 12:06)</a>:</h4>
<p><span class="user-mention silent" data-user-id="312719">Xavier Denis</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246837649">said</a>:</p>
<blockquote>
<p>6 of one, half a dozen of the other...</p>
</blockquote>
<p>?</p>



<a name="246837678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837678">(Jul 22 2021 at 12:07)</a>:</h4>
<p>well i feel like if drop were eager the rules governing its safety would be simpler</p>



<a name="246837714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837714">(Jul 22 2021 at 12:07)</a>:</h4>
<p><span class="user-mention silent" data-user-id="338379">Giacomo Stevanato</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246837670">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="312719">Xavier Denis</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246837649">said</a>:</p>
<blockquote>
<p>6 of one, half a dozen of the other...</p>
</blockquote>
<p>?</p>
</blockquote>
<p>haha, just joking that by making drop 'simpler' to formalize it complicates other things :P</p>



<a name="246837781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837781">(Jul 22 2021 at 12:08)</a>:</h4>
<p>And, as mentioned and demonstrated, a lot of existing code can be easily proven to be unsound with easy drop.<br>
I also think that eager drop is more complicated, because my model of destructors is scope-length guards</p>



<a name="246837802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837802">(Jul 22 2021 at 12:08)</a>:</h4>
<p>ah interesting</p>



<a name="246837804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837804">(Jul 22 2021 at 12:08)</a>:</h4>
<p>Technically you're only making <code>#[may_dangle]</code> simplier to formalize</p>



<a name="246837822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837822">(Jul 22 2021 at 12:09)</a>:</h4>
<p>yea but if you formalize drop you want to formalize the complete version</p>



<a name="246837943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837943">(Jul 22 2021 at 12:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="257758">Connor Horman</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246837781">said</a>:</p>
<blockquote>
<p>And, as mentioned and demonstrated, a lot of existing code can be easily proven to be unsound with easy drop.<br>
I also think that eager drop is more complicated, because my model of destructors is scope-length guards</p>
</blockquote>
<p>interesting, because to me it's more intuitive that destructors would be run as soon as a value is dead. And I agree that there is a lot of <em>current</em> code that would be unsound in that setup but if rust had made the eager-drop choice then that code would never have been written</p>



<a name="246837951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246837951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246837951">(Jul 22 2021 at 12:10)</a>:</h4>
<p>And with that model, I'd be more prone to writing code that relies on it, including for soundness. I inherited this model from C++, and I can imagine others coming from C++ may have similar ideas.</p>



<a name="246838004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838004">(Jul 22 2021 at 12:11)</a>:</h4>
<p>yea. that also explains a lot / makes sense since Rust does value familiarity with existing languages</p>



<a name="246838115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838115">(Jul 22 2021 at 12:12)</a>:</h4>
<p>Even if people wrote unsafe code with the different drop in mind it would still be more error prone since it would be harder to determine the actual drop location of something</p>



<a name="246838123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838123">(Jul 22 2021 at 12:12)</a>:</h4>
<p>Yeah. Rust has many differences from C++, but, for the most part, it makes those differences loud and obnoxious, rather than silent and "Oh no my code is fundamentally broken".</p>



<a name="246838150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838150">(Jul 22 2021 at 12:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="338379">Giacomo Stevanato</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246838115">said</a>:</p>
<blockquote>
<p>Even if people wrote unsafe code with the different drop in mind it would still be more error prone since it would be harder to determine the actual drop location of something</p>
</blockquote>
<p>this is a good point since liveness analysis is not always super intuitive</p>



<a name="246838154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838154">(Jul 22 2021 at 12:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="338379">Giacomo Stevanato</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246837120">said</a>:</p>
<blockquote>
<p>Anyway an example of <code>unsafe</code> code that's currently sound but would be unsound if variables were eagerly dropped:</p>
<p><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">];</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">v</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">();</span><span class="w"></span>
<span class="c1">// Now `v` is no longer used, can it be dropped?</span>
<span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// This would be unsound if `v` was already dropped</span>
</code></pre></div><br>
</p>
</blockquote>
<p>Take this for example, it not clear whether <code>v.as_mut_ptr()</code> keeps <code>v</code> alive or not</p>



<a name="246838249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838249">(Jul 22 2021 at 12:14)</a>:</h4>
<p>yea</p>



<a name="246838271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838271">(Jul 22 2021 at 12:14)</a>:</h4>
<p>In any case, there is an <a href="#narrow/stream/243200-t-lang.2Fmajor-changes/topic/MCP.3A.20Allowing.20the.20compiler.20to.20eagerly.20drop.20val.E2.80.A6.20lang-team.2386">open lang-team MCP</a> for this, but many of the objections presented here would apply to that (and a few others).</p>



<a name="246838348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838348">(Jul 22 2021 at 12:15)</a>:</h4>
<p><span class="user-mention silent" data-user-id="312719">Xavier Denis</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246837943">said</a>:</p>
<blockquote>
<p>interesting, because to me it's more intuitive that destructors would be run as soon as a value is dead.</p>
</blockquote>
<p>Well, to be strict about it, the value is not dead until the destructor finishes with it. So that's a self-supporting argument that the destructor is run exactly when it should be</p>



<a name="246838420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838420">(Jul 22 2021 at 12:16)</a>:</h4>
<p>true</p>



<a name="246838598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838598">(Jul 22 2021 at 12:18)</a>:</h4>
<p>And of course, the compiler is free to reuse the storage when possible</p>



<a name="246838669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838669">(Jul 22 2021 at 12:19)</a>:</h4>
<p>the compiler is free to reuse storage before the drop is run?</p>



<a name="246838698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838698">(Jul 22 2021 at 12:19)</a>:</h4>
<p>I think that's only true if there is no drop</p>



<a name="246838766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838766">(Jul 22 2021 at 12:20)</a>:</h4>
<p>ok, phew...</p>



<a name="246838771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838771">(Jul 22 2021 at 12:20)</a>:</h4>
<p>I don't see why not (assuming <code>drop</code> doesn't touch it). But I guess that only covers some trivial cases.</p>



<a name="246838798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838798">(Jul 22 2021 at 12:20)</a>:</h4>
<p>couldn't drop read the fields being dropped?</p>



<a name="246838811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838811">(Jul 22 2021 at 12:20)</a>:</h4>
<p>Is there an annotation for that? It seems pretty hard for a drop impl to not read the data</p>



<a name="246838866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838866">(Jul 22 2021 at 12:21)</a>:</h4>
<p><code>#[may_dangle]</code> is about whether you touch the parameters of the type, not the type itself (which is always assumed to be touched)</p>



<a name="246838970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246838970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246838970">(Jul 22 2021 at 12:22)</a>:</h4>
<p>But there are definitely dropless types that can become ill formed before the end of their scope, which makes formalization hard</p>



<a name="246839075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839075">(Jul 22 2021 at 12:23)</a>:</h4>
<p>and I think some dropful types can have <code>drop</code> called when they are currently ill formed, which is even harder (this is what <code>@[may_dangle]</code> is about)</p>



<a name="246839086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839086">(Jul 22 2021 at 12:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="312719">Xavier Denis</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246838798">said</a>:</p>
<blockquote>
<p>couldn't drop read the fields being dropped?</p>
</blockquote>
<p>If the drop implementation doesn't, and the compiler can prove it, it could theoretically reuse the storage of a live value of that type.</p>



<a name="246839164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839164">(Jul 22 2021 at 12:24)</a>:</h4>
<p>Yeah, that's what I meant, but that's probably not a very useful <code>drop</code></p>



<a name="246839175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839175">(Jul 22 2021 at 12:24)</a>:</h4>
<p>is that admissible in the rust semantics?</p>



<a name="246839220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839220">(Jul 22 2021 at 12:24)</a>:</h4>
<p>I think that's the general setting that <code>#[may_dangle]</code> is working under: the function nominally takes a <code>&amp;mut self</code>, but some of the fields are invalid and cannot be touched</p>



<a name="246839226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839226">(Jul 22 2021 at 12:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="203546">Laurențiu</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246839164">said</a>:</p>
<blockquote>
<p>Yeah, that's what I meant, but that's probably not a very useful <code>drop</code></p>
</blockquote>
<p>Scope guards that just run a (stateless) closure at exit.</p>



<a name="246839228"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839228" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839228">(Jul 22 2021 at 12:24)</a>:</h4>
<p>You shouldn't be able to observe it unless you leak a pointer outside the scope and dereference it (which is unsafe)</p>



<a name="246839270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839270">(Jul 22 2021 at 12:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="257758">Connor Horman</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246839226">said</a>:</p>
<blockquote>
<p>Scope guards that just run a (stateless) closure at exit.</p>
</blockquote>
<p>Yeah, we already excluded that use case in favor of closure-taking APIs.</p>



<a name="246839307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839307">(Jul 22 2021 at 12:25)</a>:</h4>
<p>not sure what you mean there</p>



<a name="246839314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839314">(Jul 22 2021 at 12:25)</a>:</h4>
<p>Which, as mentioned, can become incredibly tedious to work with.</p>



<a name="246839361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839361">(Jul 22 2021 at 12:26)</a>:</h4>
<p>you can still do drop based guards</p>



<a name="246839383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839383">(Jul 22 2021 at 12:26)</a>:</h4>
<p>they aren't as airtight though because of leakpocalypse</p>



<a name="246839408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839408">(Jul 22 2021 at 12:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246839361">said</a>:</p>
<blockquote>
<p>you can still do drop based guards</p>
</blockquote>
<p>With eager drop? How?</p>



<a name="246839417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839417">(Jul 22 2021 at 12:26)</a>:</h4>
<p>Well, those are one of the arguments against eager drops</p>



<a name="246839443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839443">(Jul 22 2021 at 12:27)</a>:</h4>
<p>couldn't you also perform a side-effect without reading at all?</p>



<a name="246839458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839458">(Jul 22 2021 at 12:27)</a>:</h4>
<p>I think that's what connor meant</p>



<a name="246839463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839463">(Jul 22 2021 at 12:27)</a>:</h4>
<p>though I suppose you would extend 'doesn't read' to 'doesn't read and is pure'</p>



<a name="246839475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839475">(Jul 22 2021 at 12:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="257758">Connor Horman</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246839314">said</a>:</p>
<blockquote>
<p>Which, as mentioned, can become incredibly tedious to work with.</p>
</blockquote>
<p>Yeah, I mean not being able to use <code>return</code> would be a deal-breaker.</p>



<a name="246839485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839485">(Jul 22 2021 at 12:28)</a>:</h4>
<p>I don't think it has to be pure, it's guaranteed to be called exactly once</p>



<a name="246839526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839526">(Jul 22 2021 at 12:28)</a>:</h4>
<p>*at most once</p>



<a name="246839532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839532">(Jul 22 2021 at 12:28)</a>:</h4>
<p><span class="user-mention silent" data-user-id="312719">Xavier Denis</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246839443">said</a>:</p>
<blockquote>
<p>couldn't you also perform a side-effect without reading at all?</p>
</blockquote>
<p>You can, but then it doesn't matter if the storage was reused or not</p>



<a name="246839564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839564">(Jul 22 2021 at 12:28)</a>:</h4>
<p><span class="user-mention silent" data-user-id="203546">Laurențiu</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246839475">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="257758">Connor Horman</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246839314">said</a>:</p>
<blockquote>
<p>Which, as mentioned, can become incredibly tedious to work with.</p>
</blockquote>
<p>Yeah, I mean not being able to use <code>return</code> would be a deal-breaker.</p>
</blockquote>
<p>The nesting too, and the other restrictions on closures that aren't present with a scope-based api.</p>



<a name="246839622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839622">(Jul 22 2021 at 12:29)</a>:</h4>
<p>And not being able to do early drops</p>



<a name="246839698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839698">(Jul 22 2021 at 12:30)</a>:</h4>
<p>Closures can't partially borrow values, IIRC, so if you mutably borrow a field of self to invoke the closure, then you can't touch anything else inside <code>self</code> during the closure</p>



<a name="246839739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839739">(Jul 22 2021 at 12:30)</a>:</h4>
<p>coming 2021</p>



<a name="246839852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839852">(Jul 22 2021 at 12:32)</a>:</h4>
<p>well the good news (for me) is that my verification tool currently doesn't handle drop and just forbids any <code>Drop</code> impls</p>



<a name="246839896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839896">(Jul 22 2021 at 12:32)</a>:</h4>
<p>much simpler that way :P</p>



<a name="246839925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839925">(Jul 22 2021 at 12:32)</a>:</h4>
<p>I think Drop is currently uncharted territory in rust formalization</p>



<a name="246839944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246839944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246839944">(Jul 22 2021 at 12:32)</a>:</h4>
<p>I'd disagree, it's probably one of the easiest things to specify.</p>



<a name="246840010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840010">(Jul 22 2021 at 12:33)</a>:</h4>
<p>yea its currently unformalized in RustBelt because of may_dangle (in part at least)</p>



<a name="246840023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840023">(Jul 22 2021 at 12:33)</a>:</h4>
<p>The fact that <code>drop</code> is a magic function that can't be called multiple times and lies about its type signature is one major issue</p>



<a name="246840038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840038">(Jul 22 2021 at 12:33)</a>:</h4>
<p>^</p>



<a name="246840094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840094">(Jul 22 2021 at 12:34)</a>:</h4>
<p>but also the fact that you have 'borrowed ownership' of things that aren't there but claim (through their types) to be</p>



<a name="246840124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840124">(Jul 22 2021 at 12:34)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246840023">said</a>:</p>
<blockquote>
<p>The fact that <code>drop</code> is a magic function that can't be called multiple times and lies about its type signature is one major issue</p>
</blockquote>
<blockquote>
<p>The function <code>Drop::drop</code> or an implementation thereof shall not be named by the program.</p>
</blockquote>



<a name="246840141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840141">(Jul 22 2021 at 12:34)</a>:</h4>
<p>this is incompatible with how RustBelt (for example) currently represents ownership.. not insurmountable ofc</p>



<a name="246840171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840171">(Jul 22 2021 at 12:35)</a>:</h4>
<p>Your type can also have lifetime arguments that have expired</p>



<a name="246840196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840196">(Jul 22 2021 at 12:35)</a>:</h4>
<p>in vanilla rustbelt that's not an issue</p>



<a name="246840226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840226">(Jul 22 2021 at 12:35)</a>:</h4>
<p>why is that?</p>



<a name="246840381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840381">(Jul 22 2021 at 12:37)</a>:</h4>
<p>oh just that rustbelt doesn't assume that the lifetimes mentioned in types are always alive</p>



<a name="246840415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840415">(Jul 22 2021 at 12:37)</a>:</h4>
<p>but to use borrows you have to prove the lifetime is alive</p>



<a name="246840541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840541">(Jul 22 2021 at 12:38)</a>:</h4>
<p>I would really like to know what the "real" type signature of Drop::drop is, rustbelt style. In particular you have to be able to change the state of the object to some state that is not normally representable in the type: for example you can transition the guard to one that isn't guarding anymore, even though the type's invariant says that it guards a thing, and <code>drop</code> takes a <code>&amp;mut T</code> so seemingly you need it to still be a valid <code>T</code> when you are done</p>



<a name="246840586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840586">(Jul 22 2021 at 12:39)</a>:</h4>
<p>yea thats the challenge</p>



<a name="246840622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840622">(Jul 22 2021 at 12:39)</a>:</h4>
<p>because at somepoint you 'deallocate' which in rustbelt produces a value of type <code>uninit</code> but you need to recover a <code>T</code> from that</p>



<a name="246840731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840731">(Jul 22 2021 at 12:40)</a>:</h4>
<p>Didn't rustbelt prove Arc sound, or something like that? Surely they would have had to tackle Drop to do that</p>



<a name="246840740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840740">(Jul 22 2021 at 12:40)</a>:</h4>
<p>or maybe they just leaked everything</p>



<a name="246840786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840786">(Jul 22 2021 at 12:41)</a>:</h4>
<p>Rustbelt proved Arc sound? That's kinda suprising.</p>



<a name="246840806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840806">(Jul 22 2021 at 12:41)</a>:</h4>
<p>there's a <code>drop_arc</code> and <code>drop_weak</code> function</p>



<a name="246840834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840834">(Jul 22 2021 at 12:42)</a>:</h4>
<p>(See the atomic bomb for the reason)</p>



<a name="246840930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840930">(Jul 22 2021 at 12:42)</a>:</h4>
<p>why can't drop have the type <code>drop(self) {}</code>? It's still a little magic but it seems easier to understand on all fronts</p>



<a name="246840971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840971">(Jul 22 2021 at 12:43)</a>:</h4>
<p>it would be a LOT nicer tbh (on paper)</p>



<a name="246840979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840979">(Jul 22 2021 at 12:43)</a>:</h4>
<p>What happens to <code>self</code>? Also, <code>Pin</code>.</p>



<a name="246840985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840985">(Jul 22 2021 at 12:43)</a>:</h4>
<p>oh wait, that wouldn't work for <code>drop_in_place</code></p>



<a name="246840994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246840994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246840994">(Jul 22 2021 at 12:43)</a>:</h4>
<p>and pin</p>



<a name="246841021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841021">(Jul 22 2021 at 12:43)</a>:</h4>
<p>self magically disappears at the end of the function</p>



<a name="246841089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841089">(Jul 22 2021 at 12:44)</a>:</h4>
<p>What happens if you pass <code>self</code> into another function that takes ownership?</p>



<a name="246841119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841119">(Jul 22 2021 at 12:44)</a>:</h4>
<p>ok so... the type of <code>arc_drop</code> is different than what it would be in rust</p>



<a name="246841154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841154">(Jul 22 2021 at 12:45)</a>:</h4>
<p><code>(fn(∅; arc ty) → option ty)</code> ie it's <code>drop(Arc&lt;T&gt;) -&gt; Option T</code></p>



<a name="246841170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841170">(Jul 22 2021 at 12:45)</a>:</h4>
<p>I guess in that case it would be dropped or leaked by that function</p>



<a name="246841373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841373">(Jul 22 2021 at 12:46)</a>:</h4>
<p>what a magical function</p>



<a name="246841377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841377">(Jul 22 2021 at 12:46)</a>:</h4>
<p>but the general model is that the ownership is passed around, and if you don't write <code>drop(x)</code>  at the end of the function the compiler inserts a regular call to the <code>drop</code> function. The only magical part is that the end of the <code>drop</code> impl is where deallocation happens</p>



<a name="246841455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841455">(Jul 22 2021 at 12:47)</a>:</h4>
<p>well... you can definitely deallocate parts of the value during drop</p>



<a name="246841469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841469">(Jul 22 2021 at 12:47)</a>:</h4>
<p>In a formalization, I would say that the bottom of the drop impl is a call to <code>free(x)</code> that passes ownership back to the allocator</p>



<a name="246841529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841529">(Jul 22 2021 at 12:48)</a>:</h4>
<p>yea thats probably what it should be in a formalized model</p>



<a name="246841626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841626">(Jul 22 2021 at 12:49)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246841469">said</a>:</p>
<blockquote>
<p>In a formalization, I would say that the bottom of the drop impl is a call to <code>free(x)</code> that passes ownership back to the allocator</p>
</blockquote>
<p>How would this work arround things that just <code>drop</code> things in-place (<code>Rc</code>, <code>Arc</code>), or drop things then explicitly deallocate them (like <code>Box</code>)</p>



<a name="246841817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841817">(Jul 22 2021 at 12:50)</a>:</h4>
<p>The thing that is getting freed is just the memory of <code>x</code> itself. Other stuff like Box::drop would be handled by compiler inserted drop calls, and Rc and Arc are doing their own memory management</p>



<a name="246841932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246841932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246841932">(Jul 22 2021 at 12:51)</a>:</h4>
<p>maybe it's less mind bending to say that <code>drop</code> has the type <code>drop(&amp;move T) {}</code></p>



<a name="246842034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246842034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246842034">(Jul 22 2021 at 12:52)</a>:</h4>
<p>it's still behind a pointer, but it's passing ownership into the function and at the end of the function the memory pointed to is freed</p>



<a name="246842124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246842124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246842124">(Jul 22 2021 at 12:53)</a>:</h4>
<p>actually I guess it would just be uninit stack memory most of the time</p>



<a name="246842483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246842483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246842483">(Jul 22 2021 at 12:56)</a>:</h4>
<p>And when it isn't, what happens then? What exactly does it mean to implicitly free memory that may be managed explicitly?</p>



<a name="246842683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246842683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246842683">(Jul 22 2021 at 12:58)</a>:</h4>
<p>On second thought, it's not really a call to <code>free</code> at all. It's just passing ownership of the raw memory to whoever the ambient memory manager is, for example the stack frame or the box in which this value is contained. <code>Box::drop</code> will actually call <code>free</code> , but that's because it explicitly says so in the drop impl, not because of compiler magic</p>



<a name="246842985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246842985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246842985">(Jul 22 2021 at 13:00)</a>:</h4>
<p>Hm, I wonder how that works for Box. The call to free has to happen after the contents of the box are dropped, because the allocator wants to receive only raw memory, not a <code>T</code>. Does <code>#[may_dangle]</code> affect whether the call to <code>Box&lt;T&gt;::drop</code> happens before or after <code>T::drop</code>?</p>



<a name="246843120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246843120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246843120">(Jul 22 2021 at 13:01)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[stable(feature = </span><span class="s">"rust1"</span><span class="cp">, since = </span><span class="s">"1.0.0"</span><span class="cp">)]</span><span class="w"></span>
<span class="k">unsafe</span><span class="w"> </span><span class="k">impl</span><span class="o">&lt;</span><span class="cp">#[may_dangle]</span><span class="w"> </span><span class="n">T</span>: <span class="o">?</span><span class="nb">Sized</span><span class="p">,</span><span class="w"> </span><span class="n">A</span>: <span class="nc">Allocator</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">drop</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// FIXME: Do nothing, drop is currently performed by compiler.</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>welp</p>



<a name="246845868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246845868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246845868">(Jul 22 2021 at 13:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246842985">said</a>:</p>
<blockquote>
<p>Hm, I wonder how that works for Box. The call to free has to happen after the contents of the box are dropped, because the allocator wants to receive only raw memory, not a <code>T</code>. Does <code>#[may_dangle]</code> affect whether the call to <code>Box&lt;T&gt;::drop</code> happens before or after <code>T::drop</code>?</p>
</blockquote>
<p><code>Box</code> is special cased in the compiler because you can actually move out of a box and then move back in, or even partially move out of it. The compiler keeps track of its contents as if they were in a local binding. When the <code>Box</code> goes out of scope it will make sure to drop only what is currently initialized and then it will call <code>free</code> (technically it's <code>Allocator::deallocate</code>).</p>



<a name="246846190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246846190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246846190">(Jul 22 2021 at 13:28)</a>:</h4>
<p>After some testing, it seems like in <code>A&lt;B&lt;C&gt;&gt;</code>, it will first call <code>A::drop</code> then <code>B::drop</code> then <code>C::drop</code>, then <code>free(c)</code> then <code>free(b)</code> then <code>free(a)</code> (assuming there are heap allocations like Vec or Box in between)</p>



<a name="246846233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246846233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246846233">(Jul 22 2021 at 13:28)</a>:</h4>
<p>and <code>may_dangle</code> doesn't affect drop order</p>



<a name="246846531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246846531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246846531">(Jul 22 2021 at 13:30)</a>:</h4>
<p>So the state that <code>A::drop</code> is supposed to leave the object in is a bit mysterious to me. It needs to have cleaned up its invariants, but not any nested invariants for <code>B</code> and <code>C</code> because their drop impls haven't been called yet... but they might not even be valid if <code>may_dangle</code> is set...</p>



<a name="246846957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246846957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246846957">(Jul 22 2021 at 13:34)</a>:</h4>
<p>I guess you could say that <code>A::drop</code> should leave the type in a state that matches the nominal type of <code>A</code> as perceived by the compiler, i.e. the conjunction of all the fields of the struct, which the compiler knows how to deal with, but not things like outstanding reference counts that are invariants of the type not represented explicitly in the type system</p>



<a name="246884825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246884825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246884825">(Jul 22 2021 at 18:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F/near/246846531">said</a>:</p>
<blockquote>
<p>So the state that <code>A::drop</code> is supposed to leave the object in is a bit mysterious to me. It needs to have cleaned up its invariants, but not any nested invariants for <code>B</code> and <code>C</code> because their drop impls haven't been called yet... but they might not even be valid if <code>may_dangle</code> is set...</p>
</blockquote>
<p>AFAIK <code>may_dangle</code> doesn't allow <code>B&lt;C&gt;</code> to become invalid if it has drop machinery</p>



<a name="246885087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20Drop%20run%20at%20end%20of%20lexical%20scope%3F/near/246885087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20Drop.20run.20at.20end.20of.20lexical.20scope.3F.html#246885087">(Jul 22 2021 at 18:11)</a>:</h4>
<p>It pretty much asserts the the <code>Drop</code> implementation in which it is used will only move or drop values of the type marked with <code>#[may_dangle]</code> thus if they have <code>drop</code> machinery that still counts as an use and those types must be valid then</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>